﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Diploma.DAL.Hibernate.Abstract;
using NHibernate;
using NHibernate.Cfg;

namespace Diploma.DAL.Hibernate.Concrete
{
    public class TestSessionFactory : ITestSessionFactory
    {
        #region Fields

        private readonly string _configFileName;
        private ISessionFactory _sessionFactory = null;

        #endregion

        #region Constructors

        public TestSessionFactory(string configFileName)
        {
            this._configFileName = configFileName;
        }

        #endregion

        #region ISessionFactory Members

        public ISession CreateSession()
        {
            if(this._sessionFactory == null)
            {
                this._sessionFactory = CreateSessionFactory();
            }
            return this._sessionFactory.OpenSession();
        }

        #endregion

        #region Helpers

        private ISessionFactory CreateSessionFactory()
        {
            Configuration configuration = new Configuration();
            configuration.Configure(this._configFileName);
            return configuration.BuildSessionFactory();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (this._sessionFactory != null)
            {
                this._sessionFactory.Dispose();
            }
        }

        #endregion
    }
}
